package com.quectel.core.module.officialaccounts.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.officialaccounts.dao.OfficialAccountsBindingDao;
import com.quectel.core.module.officialaccounts.dto.OfficialAccountsBindingDto;
import com.quectel.core.module.officialaccounts.entity.OfficialAccountsBindingEntity;
import com.quectel.core.module.officialaccounts.service.OfficialAccountsBindingService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-14 14:23:37
 */
@DubboService
public class OfficialAccountsBindingServiceImpl implements OfficialAccountsBindingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OfficialAccountsBindingServiceImpl.class);

    private static final String OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID:";

    private static final String OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_4_PARAMS = RedisCacheConstants.PROJECT_NAME + "OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_4_FUNC:";

    @Autowired
    private OfficialAccountsBindingDao officialAccountsBindingDao;


    @Override
    public List<OfficialAccountsBindingDto> selectCacheBy(Byte personType, Long personId, Byte type, String appId) {
        return CacheKit.cacheToRedis(
                () -> selectBy(personType, personId, type, appId),
                OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_4_PARAMS + ":" + personType + ":" + personId + ":" + type + ":" + appId,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<OfficialAccountsBindingDto> selectBy(Byte personType, Long personId, Byte type, String appId) {
        List<OfficialAccountsBindingEntity> list = officialAccountsBindingDao.selectList(new LambdaQueryWrapper<OfficialAccountsBindingEntity>()
                .eq(OfficialAccountsBindingEntity::getPersonType, personType)
                .eq(OfficialAccountsBindingEntity::getPersonId, personId)
                .eq(OfficialAccountsBindingEntity::getType, type)
                .eq(OfficialAccountsBindingEntity::getAppId, appId)

        );
        return CopyUtils.copyList(list, OfficialAccountsBindingDto.class);
    }

    @Override
    public OfficialAccountsBindingDto selectBy(Byte type, String appId, String openId, Byte personType) {
        OfficialAccountsBindingEntity entity = officialAccountsBindingDao.selectOne(new LambdaQueryWrapper<OfficialAccountsBindingEntity>()
                .eq(OfficialAccountsBindingEntity::getType, type)
                .eq(OfficialAccountsBindingEntity::getAppId, appId)
                .eq(OfficialAccountsBindingEntity::getOpenId, openId)
                .eq(OfficialAccountsBindingEntity::getPersonType, personType)
                .last(SqlConstants.MYSQL_LIMIT_ONE)

        );
        return CopyUtils.copyObj(entity, OfficialAccountsBindingDto.class);
    }


    @Override
    public OfficialAccountsBindingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public OfficialAccountsBindingDto selectById(Long id) {
        OfficialAccountsBindingEntity entity = officialAccountsBindingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, OfficialAccountsBindingDto.class));
    }

    @Override
    public List<OfficialAccountsBindingDto> queryList(Map<String, Object> params) {
        List<OfficialAccountsBindingEntity> list = officialAccountsBindingDao.queryList(params);
        List<OfficialAccountsBindingDto> result = CopyUtils.copyList(list, OfficialAccountsBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private OfficialAccountsBindingDto paddingField(OfficialAccountsBindingDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return officialAccountsBindingDao.queryTotal(params);
    }

    @Override
    public Long save(OfficialAccountsBindingDto dto) {
        OfficialAccountsBindingEntity entity = CopyUtils.copyObj(dto, OfficialAccountsBindingEntity.class);
        entity.setId(Snowflake.nextId());
        officialAccountsBindingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(OfficialAccountsBindingDto dto) {
        OfficialAccountsBindingEntity entity = CopyUtils.copyObj(dto, OfficialAccountsBindingEntity.class);
        officialAccountsBindingDao.updateById(entity);
        CacheKit.invalidRedisCache(OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(OfficialAccountsBindingDto dto) {
        OfficialAccountsBindingEntity entity = CopyUtils.copyObj(dto, OfficialAccountsBindingEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, officialAccountsBindingDao);

        CacheKit.invalidRedisCache(OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        officialAccountsBindingDao.deleteById(id);

        CacheKit.invalidRedisCache(OFFICIAL_ACCOUNTS_BINDING_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
